home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dicecache / lib.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  4KB  |  176 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  LIB.C
  9.  *
  10.  *  Basic Library Resource Handling
  11.  *
  12.  *  NOTE: all data declarations should be initialized since we skip
  13.  *      normal C startup code (unless initial value is don't care)
  14.  */
  15.  
  16. #include "defs.h"
  17. #include <exec/tasks.h>
  18. #include <dos/dosextens.h>
  19.  
  20. typedef struct CommandLineInterface CLI;
  21.  
  22. Prototype LibCall Library *LibInit(long);
  23. Prototype LibCall Library *LibOpen(long, Library *);
  24. Prototype LibCall long LibClose(long, Library *);
  25. Prototype LibCall long LibExpunge(long, Library *);
  26. Prototype char *CallTaskName(void);
  27. Prototype Library *LibBase;
  28.  
  29.  
  30. Library *LibBase = NULL;    /*  Library Base pointer    */
  31. long    SegList  = 0;
  32. long    SysBase  = NULL;
  33. struct DosLibrary    *DOSBase  = NULL;
  34.  
  35.  
  36. /*
  37.  *    The Initialization routine is given only a seglist pointer.  Since
  38.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  39.  *    and return either NULL or the library pointer.  Exec has Forbid()
  40.  *    for us during the call.
  41.  *
  42.  *    If you have an extended library structure you must specify the size
  43.  *    of the extended structure in MakeLibrary().
  44.  */
  45.  
  46. LibCall Library *
  47. LibInit(segment)
  48. long segment;
  49. {
  50.     Library *lib;
  51.     static const long Vectors[] = {
  52.     (long)ALibOpen,
  53.     (long)ALibClose,
  54.     (long)ALibExpunge,
  55.     (long)NULL,
  56.  
  57.     (long)DiceCacheOpen,
  58.     (long)DiceCacheClose,
  59.     (long)DiceCacheSeek,
  60.     (long)DiceCacheGetSuffixes,
  61.     (long)DiceCacheAddSuffix,
  62.     (long)DiceCacheRemSuffix,
  63.     (long)DiceCacheFlush,
  64.     (long)DiceCacheSet,
  65.     (long)DiceCacheGet,
  66.     (long)DiceCacheEnable,
  67.     (long)DiceCacheDisable,
  68.  
  69.     -1
  70.     };
  71.     SysBase = *(long *)4;
  72.     DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 0);
  73.  
  74.     LibBase = lib = MakeLibrary((APTR)Vectors,NULL,NULL,sizeof(Library),NULL);
  75.     lib->lib_Node.ln_Type = NT_LIBRARY;
  76.     lib->lib_Node.ln_Name = LibName;
  77.     lib->lib_Flags = LIBF_CHANGED|LIBF_SUMUSED;
  78.     lib->lib_Version  = 37;
  79.     lib->lib_Revision = 5;
  80.     lib->lib_IdString = (APTR)LibId;
  81.     SegList = segment;
  82.     AddLibrary(lib);
  83.  
  84.     InitC();
  85.  
  86.     return(lib);
  87. }
  88.  
  89. /*
  90.  *    Open is given the library pointer and the version request.  Either
  91.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  92.  *    Exec has Forbid() for us during the call.
  93.  */
  94.  
  95. LibCall Library *
  96. LibOpen(version, lib)
  97. Library *lib;
  98. long version;
  99. {
  100.     ++lib->lib_OpenCnt;
  101.     lib->lib_Flags &= ~LIBF_DELEXP;
  102.     return(lib);
  103. }
  104.  
  105. /*
  106.  *    Close is given the library pointer and the version request.  Be sure
  107.  *    not to decrement the open count if already zero.    If the open count
  108.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  109.  *    and return the seglist.  Otherwise we return NULL.
  110.  *
  111.  *    Note that this routine never sets LIBF_DELEXP on its own.
  112.  *
  113.  *    Exec has Forbid() for us during the call.
  114.  */
  115.  
  116. LibCall long
  117. LibClose(dummy, lib)
  118. long dummy;
  119. Library *lib;
  120. {
  121.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  122.     return(NULL);
  123.     if (lib->lib_Flags & LIBF_DELEXP)
  124.     return(LibExpunge(0, lib));
  125.     return(NULL);
  126. }
  127.  
  128. /*
  129.  *    We expunge the library and return the Seglist ONLY if the open count
  130.  *    is zero.    If the open count is not zero we set the DELAYED-EXPUNGE
  131.  *    flag and return NULL.
  132.  *
  133.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  134.  *    might be called from the memory allocator and thus we CANNOT DO A
  135.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  136.  *
  137.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  138.  *    therefore freeze if we called it ourselves.  As far as I can tell
  139.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  140.  *    below.
  141.  */
  142.  
  143. LibCall long
  144. LibExpunge(dummy, lib)
  145. long dummy;
  146. Library *lib;
  147. {
  148.     ObtainSemaphore(&SemLock);
  149.     if (lib->lib_OpenCnt) {
  150.     lib->lib_Flags |= LIBF_DELEXP;
  151.     return(NULL);
  152.     }
  153.     Remove(&lib->lib_Node);
  154.     UnInitC();
  155.     FreeMem((char *)lib-lib->lib_NegSize, lib->lib_NegSize+lib->lib_PosSize);
  156.     if (DOSBase) {
  157.     CloseLibrary((Library *)DOSBase);
  158.     DOSBase = NULL;
  159.     }
  160.     ReleaseSemaphore(&SemLock);
  161.     return((long)SegList);
  162. }
  163.  
  164. char *
  165. CallTaskName()
  166. {
  167.     struct Process *proc = (struct Process *)FindTask(NULL);
  168.  
  169.     if (proc->pr_CLI) {
  170.     char *ptr = (char *)(((CLI *)(proc->pr_CLI << 2))->cli_CommandName << 2);
  171.     return(ptr + 1);
  172.     }
  173.     return(proc->pr_Task.tc_Node.ln_Name);
  174. }
  175.  
  176.